package com.example.ordersystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ordersystem.entity.Food;
import com.example.ordersystem.entity.Order;
import com.example.ordersystem.entity.OrdersFood;
import com.example.ordersystem.entity.vo.FoodVo;
import com.example.ordersystem.entity.vo.FoodVo1;
import com.example.ordersystem.entity.vo.OrderVo;
import com.example.ordersystem.mapper.FoodMapper;
import com.example.ordersystem.mapper.OrderMapper;
import com.example.ordersystem.mapper.OrdersFoodMapper;
import com.example.ordersystem.service.OrderService;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Resource
    OrderMapper orderMapper;
    @Resource
    OrdersFoodMapper ordersFoodMapper;
    @Resource
    FoodMapper foodMapper;

    /*
      用户下单
     */
    @Override
    public boolean add(List<FoodVo> foodVoList, Order order) {
        //判断有没有超卖
        for(FoodVo foodVo:foodVoList)
        {
            Food food = foodMapper.selectById(foodVo.getId());
            int stock = Integer.parseInt(food.getStock());
            int num = Integer.parseInt(foodVo.getNum());
            if(num>stock)
            {
                return false;
            }
        }
        long orderId = System.currentTimeMillis();
        order.setOrderId(String.valueOf(orderId));
        for(FoodVo foodVo:foodVoList)
        {
            OrdersFood ordersFood=new OrdersFood();
            ordersFood.setFood_id(foodVo.getId());
            ordersFood.setOrder_id(String.valueOf(orderId));
            ordersFood.setNum(foodVo.getNum());
            ordersFoodMapper.insert(ordersFood);
        }
        orderMapper.insert(order);
        return true;
    }

    /**
     * 用户查看自己的全部订单
     */
    @Override
    public Page<Order> viewUserList(String userId,String currentPage) {
        QueryWrapper<Order> wrapper=new QueryWrapper<Order>().
                eq("userId",userId);
        return orderMapper.selectPage(new Page<>(Integer.parseInt(currentPage), 10), wrapper);

    }

    /**
     * 配送员查看所有的未配送订单
     */
    @Override
    public List<Order> viewAll() {
        QueryWrapper<Order> wrapper=new QueryWrapper<Order>().
                eq("progress","1");
         return  orderMapper.selectList(wrapper);
    }

    /**
     * 配送员处理订单
     */
    @Override
    public void handle(String orderId,String endTime) {
        QueryWrapper<Order> wrapper=new QueryWrapper<Order>().
                eq("orderId",orderId);
        Order order=new Order();
        order.setProgress("3");
        order.setEndTime(endTime);
        orderMapper.update(order,wrapper);
    }

    /**
     * 餐厅管理员查看所有订单
     */
    @Override
    public Page<Order> adminViewAll(String currentPage) {
        return orderMapper.selectPage(new Page<Order>(Integer.parseInt(currentPage),10),null);
    }

    /**
     *骑手接单
     */
    @Override
    public void receiveOrder(String orderId, String courierId) {
        QueryWrapper<Order> wrapper=new QueryWrapper<Order>().
                eq("orderId",orderId);
             Order order=new Order();
             order.setProgress("2");
             order.setCourierId(courierId);
             orderMapper.update(order,wrapper);
    }

    /**
     * 配送员查看自己的订单接收列表
     */
    @Override
    public List<Order> viewReceiveList(String courierId) {
        QueryWrapper<Order> wrapper=new QueryWrapper<Order>().
                eq("courierId",courierId).
                eq("progress","2");
        return orderMapper.selectList(wrapper);
    }

    /**
     * 配送员查看自己的处理订单记录
     */
    @Override
    public List<Order> viewHandleList(String courierId) {
        QueryWrapper<Order> wrapper=new QueryWrapper<Order>().
                eq("courierId",courierId).
                eq("progress","3");
        return orderMapper.selectList(wrapper);
    }

    /**
     * 查询订单详细信息
     */
    @Override
    public List<OrderVo> viewDetail(String orderId) {
        return orderMapper.viewDetail(orderId);
    }

}
